package cn.acewill.yxd.yunxundian.pubClass;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.RMain;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.rJson;
import cn.acewill.yxd.yunxundian.model.sType;

/**
 * Created by user_001 on 2017/12/20.
 */

public class reportQuery {
    ArrayList<RMain> rMains;

    public reportQuery(int schid){
        try {
            DbManager db = x.getDb(new YXDAPP().getDaoConfig());
            Schedule sMain = db.selector(Schedule.class).where("ID", "=", schid).findFirst();
            sType type = db.selector(sType.class).where("ID", "=", sMain.getSTypeID()).findFirst();
            rJson rmian = db.selector(rJson.class).where("ID", "=", type.getReportId()).findFirst();
            String json = rmian.getjSon();
            Gson gson = new Gson();
            Type typeList = new TypeToken<ArrayList<RMain>>(){}.getType();
            rMains = gson.fromJson(pubClass.convertToArrayJsonNormal(json), typeList);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public RMain.TablesBean.RowsBean.Q2Bean getRowByRowId(int rowid) {
        if(rMains != null){
            for (int i = 0; i < rMains.size(); i++) {
                List<RMain.TablesBean> tb = rMains.get(i).getTables();
                for (RMain.TablesBean t : tb) {
                    List<RMain.TablesBean.RowsBean> rbs = t.getRows();
                    for (RMain.TablesBean.RowsBean rb : rbs) {
                        if (rb.getQ2().getID() == rowid) {
                            return rb.getQ2();
                        }
                    }
                }
            }
        }
        return null;
    }
}
